<template>
    <div class="app-container" v-loading.fullscreen.lock="loading">
        <div style="margin-top: 10px;">
            <el-form label-width="120px">
                <el-row>
                    <el-col :span="4">
                        <el-form-item label="学号" label-width="40px">
                            <el-input size="mini" v-model="search.stuNo"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label-width="10px">
                            <el-button icon="el-icon-search" type="primary" size="mini" @click="doSearch">搜索</el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="doRefresh">重置</el-button>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="班级" label-width="40px">
                            <el-select style="width: 100%;" size="mini" @change="changeSelect"
                                v-model="search.className">
                                <el-option v-for="(v, i) in class_list" :key="i" :label="v.className"
                                    :value="v.className"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="在校状态" label-width="80px">
                            <el-select size="mini" @change="changeStatus" v-model="search.status" placeholder="在校状态"
                                clearable>
                                <el-option v-for="dict in dict.type.srs_stu_status" :key="dict.value"
                                    :label="dict.label" :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="2">
                        <el-form-item label="" label-width="10px">
                            <el-button type="success" size="mini" @click="exportV = true">导出</el-button>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="学年" label-width="40px">
                            <el-select size="mini" @change="changeYear" v-model="search.stuYearName" placeholder="选择学年"
                                clearable>
                                <el-option :key="i" v-for="(v, i) in stu_year_list" :value="v.stuYearName"
                                    :label="v.stuYearName"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="" label-width="10px">
                            <el-button size="mini" @click="majorV = true;">导出专业排名</el-button>
                        </el-form-item>
                    </el-col>
                    <el-col :span="16">
                        <pagination style="margin-bottom: 40px;margin-top: -10px;position:relative;" v-show="total > 0"
                            :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                            @pagination="listOwnStu" />
                    </el-col>

                </el-row>

            </el-form>
        </div>

        <el-table v-loading="loading" :data="stu_list" style="width:100%;margin-top: -20px;">
            <el-table-column v-if="search.className != '' || search.status != '' || search.stuYearName != ''" label="#"
                align="center" width="80">
                <template slot-scope="scope">
                    {{ scope.$index + 1 }}
                </template>
            </el-table-column>
            <el-table-column prop="className" label="班级" align="center" width="280" />
            <el-table-column prop="stuYearName" label="学年" align="center" width="140" />
            <el-table-column prop="stuNo" label="学号" align="center" width="120" />
            <el-table-column prop="stuName" label="学生姓名" align="center" width="120" />
            <el-table-column prop="cphScore" label="综合素质总成绩" align="center" width="120" />
            <el-table-column prop="iamScore" label="思想品德" align="center" width="80" />
            <el-table-column prop="classtwoScore" label="第二课堂" align="center" width="80" />
            <el-table-column prop="ceScore" label="综合评价" align="center" width="80" />
            <el-table-column prop="sportScore" label="体能素质" align="center" width="80" />
            <el-table-column prop="stuScore" label="平均学分绩" align="center" width="80" />
            <el-table-column prop="classRank" label="班级排名" align="center" width="80" />
            <el-table-column prop="majorRank" label="专业排名" align="center" width="80" />
            <el-table-column prop="carderName" label="职位" align="center" width="80" />
            <el-table-column label="学生状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.srs_stu_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column width="90" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-refresh"
                        @click="doInitPwdOne(scope.row)">初始化密码</el-button>
                    <el-button size=mini type=text icon=el-icon-refresh @click=openDialog(scope.row)>职位设置</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="选择导出" :visible.sync="exportV" width="600px" append-to-body>
            <el-form label-width="120px">
                <el-form-item label="学年">
                    <el-select id="stuYearId" size="mini" style="width: 400px;" v-model="exportForm.stuYearId"
                        placeholder="请选择学年">
                        <el-option :key="i" v-for="(v, i) in stu_year_list" :value="v.id"
                            :label="v.stuYearName"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="班级">
                    <el-select style="width: 400px;" size="mini" v-model="exportForm.classId">
                        <el-option v-for="(v, i) in class_list" :key="i" :label="v.className"
                            :value="v.classId"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="">
                    <el-button size="mini" @click="doExport">导出</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

        <el-dialog title="专业导出" :visible.sync="majorV" width="600px" append-to-body>
            <el-form label-width="120px">
                <el-form-item label="学年">
                    <el-select id="stuYearId" size="mini" style="width: 400px;" v-model="majorForm.stuYearId"
                        placeholder="请选择学年">
                        <el-option :key="i" v-for="(v, i) in stu_year_list" :value="v.id"
                            :label="v.stuYearName"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="专业">
                    <el-select style="width: 400px;" size="mini" v-model="majorForm.majorId">
                        <el-option v-for="(v, i) in major_list" :key="i" :label="v.majorName"
                            :value="v.majorId"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="年级">
                    <el-select style="width: 400px;" size="mini" v-model="majorForm.gradeId">
                        <el-option v-for="(v, i) in grade_list" :key="i" :label="v.gradeName"
                            :value="v.gradeId"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="">
                    <el-button size="mini" @click="majorExport">导出</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

        <el-dialog :visible.sync="lookScoreV" title="学生综合素质成绩">
            <el-table :data="score_list">
                <el-table-column prop="stuYearName" label="学年" align="center" />
                <el-table-column prop="totalScore" label="成绩" align="center" />
            </el-table>
        </el-dialog>
        <student-cadre :visible="dialogVisible" :stu-no="stuNo" :carder-id="carderId" @close="handleDialogClose"
            @update:visible="handleDialogClose" @cardersName="cardersName" />
    </div>
</template>
<script>

import { listOwnStu, listOwnAllStu, getStuScore, listOwnMajor, listOwnGrade } from "@/api/stuCQS/basedata/teacher";
import { isEmpty } from "@/api/helpFunc";
import { listOwnClass } from "@/api/stuCQS/info-fill/stu_eva_task";
import { listCphScore, doExport } from "@/api/stuCQS/info-fill/cqScore";
import * as XLSX from 'xlsx';
import { workbook2blob, openDownloadDialog, file2Xce } from "@/api/helpFunc";
import { listAllYear as listYear } from "@/api/stuCQS/basedata/year";
import { initOnePwd } from "@/api/stuCQS/basedata/student";
import StudentCadre from '@/components/StudentCadre.vue'


export default {
    name: "teacherOwn",
    dicts: ['srs_stu_status'],
    components: {
        StudentCadre
    },
    data() {
        return {
            dialogVisible: false,
            stuNo: "",
            carderId: null,
            exportForm: {
                stuYearId: null,
                classId: null,
                majorId: null
            },
            exportV: false,
            stu_year_list: [],

            queryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            stu_list: [],
            total: 0,
            loading: false,
            search: {
                stuNo: "",
                className: "",
                status: "",
                stuYearName: ""
            },
            class_list: [],
            lookScoreV: false,
            score_list: [],
            all_stu_list: [],
            major_list: [],
            grade_list: [],

            majorV: false,
            majorForm: {
                stuYearId: null,
                majorId: null,
                gradeId: null
            },

        }
    },
    created() {
        this.listOwnMajor();
        this.listOwnGrade();
        this.listYear();
        this.listOwnStu();
        this.listOwnClass();
        this.listAllStu();
    },
    methods: {

        openDialog(row) {
            this.dialogVisible = true;
            console.log(this.dialogVisible);
            this.stuNo = row.stuNo;
            this.carderId = row.carderId
            console.log(this.carderId);
        },
        handleDialogClose(newValue) {
            this.dialogVisible = newValue;
        },
        cardersName(value) {
            this.stu_list.forEach(i => {
                if (i.stuNo === value.stuNo) {
                    i.carderName = value.carder
                    i.carderId = value.carderId
                }
            })
        },
        async listOwnGrade() {
            let res = await listOwnGrade();
            if (res.code == 200) {
                this.grade_list = [...res.data];
            }
        },
        async listOwnMajor() {
            let res = await listOwnMajor();
            if (res.code == 200) {
                this.major_list = [...res.data];
            }
        },
        async majorExport() {
            if (isEmpty(this.majorForm.stuYearId)) {
                this.$message("请选择学年");
                return;
            }
            if (isEmpty(this.majorForm.majorId)) {
                this.$message("请选择专业");
                return;
            }
            if (isEmpty(this.majorForm.gradeId)) {
                this.$message("请选择年级");
                return;
            }

            this.loading = true;
            let res = await doExport(this.majorForm);
            this.loading = false;
            if (res.code == 200) {
                let data = res.data;
                const header = ['1学年', '2学院', '3专业', '4班级', '5学号', '6姓名', '7思想品德', '8体能素质', '9综合评价', '10科学文化素质', '11第二课堂', '12总成绩', '13班级排名', '14专业排名'];
                const header_zh = {
                    '1学年': '1学年',
                    '2学院': '2学院',
                    '3专业': '3专业',
                    '4班级': '4班级',
                    '5学号': '5学号',
                    '6姓名': '6姓名',
                    '7思想品德': '7思想品德',
                    '8体能素质': '8体能素质',
                    '9综合评价': '9综合评价',
                    '10科学文化素质': '10科学文化素质',
                    '11第二课堂': '11第二课堂',
                    '12总成绩': '12总成绩',
                    '13班级排名': '13班级排名',
                    '14专业排名': '14专业排名',
                    '15学生状态': '15学生状态',
                    '16导出时间': '16导出时间'
                };

                const arrayWithHeader = [header_zh, ...data];

                var sheet1 = XLSX.utils.json_to_sheet(arrayWithHeader,
                    {
                        header: header,
                        skipHeader: true
                    });
                sheet1["!cols"] = [
                    { wch: 15 },
                    { wch: 20 },
                    { wch: 20 },
                    { wch: 25 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                ];

                // 创建一个新的空的workbook
                var wb = XLSX.utils.book_new();
                // 为每一个工作簿设置名称并添加到workbook（excel表）中
                XLSX.utils.book_append_sheet(wb, sheet1, "综合成绩");
                const workbookBlob = workbook2blob(wb);  // 辅助函数workbook2blob
                // 下载文档并添加文件名称
                openDownloadDialog(workbookBlob, "专业综合成绩.xlsx");  // 辅助函数openDownloadDialog

            }
        },

        async doExport() {
            if (isEmpty(this.exportForm.stuYearId)) {
                this.$message("请选择学年");
                return;
            }
            if (isEmpty(this.exportForm.classId)) {
                this.$message("请选择班级");
                return;
            }

            this.loading = true;
            let res = await doExport(this.exportForm);
            this.loading = false;
            if (res.code == 200) {
                let data = res.data;
                const header = ['1学年', '2学院', '3专业', '4班级', '5学号', '6姓名', '7思想品德', '8体能素质', '9综合评价', '10科学文化素质', '11第二课堂', '12总成绩', '13班级排名', '14专业排名'];
                const header_zh = {
                    '1学年': '1学年',
                    '2学院': '2学院',
                    '3专业': '3专业',
                    '4班级': '4班级',
                    '5学号': '5学号',
                    '6姓名': '6姓名',
                    '7思想品德': '7思想品德',
                    '8体能素质': '8体能素质',
                    '9综合评价': '9综合评价',
                    '10科学文化素质': '10科学文化素质',
                    '11第二课堂': '11第二课堂',
                    '12总成绩': '12总成绩',
                    '13班级排名': '13班级排名',
                    '14专业排名': '14专业排名',
                    '15学生状态': '15学生状态',
                    '16导出时间': '16导出时间'
                };

                const arrayWithHeader = [header_zh, ...data];

                var sheet1 = XLSX.utils.json_to_sheet(arrayWithHeader,
                    {
                        header: header,
                        skipHeader: true
                    });
                sheet1["!cols"] = [
                    { wch: 15 },
                    { wch: 20 },
                    { wch: 20 },
                    { wch: 25 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 },
                    { wch: 12 }
                ];

                // 创建一个新的空的workbook
                var wb = XLSX.utils.book_new();
                // 为每一个工作簿设置名称并添加到workbook（excel表）中
                XLSX.utils.book_append_sheet(wb, sheet1, "综合成绩");
                const workbookBlob = workbook2blob(wb);  // 辅助函数workbook2blob
                // 下载文档并添加文件名称
                openDownloadDialog(workbookBlob, "综合成绩.xlsx");  // 辅助函数openDownloadDialog

            }
        },
        async listYear() {
            let res = await listYear();
            if (res.code == 200) {
                let data = res.data;
                this.stu_year_list = [...data];
            }
        },
        async doInitPwdOne(val) {
            let sdata = {
                username: val.stuNo
            }
            let res = await initOnePwd(sdata);
            if (res.code == 200) {
                this.$message.success(res.msg);
            }
        },
        changeYear() {
            let status = this.search.status;
            let className = this.search.className;
            let year = this.search.stuYearName;
            if (!isEmpty(year)) {
                if (!isEmpty(className)) {
                    if (!isEmpty(status)) {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.status == status && x.className == className && x.stuYearName == year) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    } else {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.className == className && x.stuYearName == year) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    }
                } else {
                    if (!isEmpty(status)) {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.status == status && x.stuYearName == year) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    } else {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.stuYearName == year) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    }
                }
            }
        },

        changeStatus() {
            let status = this.search.status;
            let className = this.search.className;
            let year = this.search.stuYearName;

            if (!isEmpty(status)) {
                if (!isEmpty(className)) {
                    if (!isEmpty(year)) {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.status == status && x.className == className && x.stuYearName == year) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    } else {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.status == status && x.className == className) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    }
                } else {

                    if (!isEmpty(year)) {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.status == status && x.stuYearName == year) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    } else {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.status == status) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];

                        return;
                    }
                }


            }
        },
        async listAllStu() {
            this.loading = true;
            let res = await listOwnAllStu();
            if (res.code == 200) {
                this.all_stu_list = [...res.data];
            }
            this.loading = false;
        },
        async lookScoreVClick(row) {
            this.loading = true;
            let stuNo = row.stuNo;
            let res = await getStuScore(stuNo);
            this.loading = false;
            if (res.code == 200) {
                this.score_list = [...res.data];
                console.log(this.score_list);
                this.lookScoreV = true;
            } else {
                this.$message.error(res.msg);
            }


        },
        async changeSelect() {
            let status = this.search.status;
            let className = this.search.className;
            let year = this.search.stuYearName;
            if (!isEmpty(className)) {
                if (!isEmpty(status)) {
                    if (!isEmpty(year)) {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.status == status && x.className == className && x.stuYearName == year) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    } else {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.status == status && x.className == className) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    }
                } else {
                    if (!isEmpty(year)) {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.className == className && x.stuYearName == year) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];
                        return;
                    } else {
                        let temp1 = [];
                        let temp2 = [...this.all_stu_list];
                        temp2.map(x => {
                            if (x.className == className) {
                                temp1.push(x);
                            }
                        });
                        this.total = 0;
                        this.queryParams.pageSize = temp1.length;
                        this.stu_list = [...temp1];

                        return;
                    }
                }

            }
        },
        doRefresh() {
            this.search.stuNo = "";
            this.search.className = "";
            this.search.status = "";
            this.search.stuYearName = "";
            this.queryParams.pageSize = 10;
            this.listOwnStu();
        },
        async doSearch() {
            let stuNo = this.search.stuNo;
            if (!isEmpty(stuNo)) {
                let temp1 = [];
                let temp2 = [...this.all_stu_list];
                temp2.map(x => {
                    if (x.stuNo == stuNo) {
                        temp1.push(x);
                    }
                });
                this.total = 0;
                this.queryParams.pageSize = temp1.length;
                this.stu_list = [...temp1];
                return;

            }
        },
        async listOwnStu() {
            this.loading = true;
            let res = await listOwnStu(this.queryParams);
            this.loading = false;
            if (res.code == 200) {
                this.stu_list = [...res.rows];
                this.total = res.total;
            }
        },
        async listOwnClass() {
            this.loading = true;
            let res = await listOwnClass();
            this.loading = false;
            if (res.code == 200) {
                this.class_list = [...res.data];
            }
        }
    }
}

</script>
